﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class User_dang_ky : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserEmail"] == null & Request.Cookies["UserEmail"]==null)
            Response.Redirect("~/User/dang-nhap.aspx");
        if(Session["UserEmail"]==null & Request.Cookies["UserEmail"]!=null)
                if(Request.Cookies["UserEmail"].Value=="")
                   Response.Redirect("~/User/dang-nhap.aspx");
        
            if(!IsPostBack)

        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetUserInForByUserEmail";
            Dal.ParameterCollection = new string[1] {"@UserEmail"};
                if(Session["UserEmail"]!=null)
                Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString()};
                if(Session["UserEmail"]==null & Request.Cookies["UserEmail"]!=null)
                    if(Request.Cookies["UserEmail"].Value!="")
                        Dal.ValueCollection = new string[1] { Request.Cookies["UserEmail"].ToString() };
            DataRow dr = Dal.GetDataRow();
            avatar.ImageUrl = "~/Images/UserAvatar/" + dr["Image"].ToString();
            txtDisplayName.Text = dr["ShowName"].ToString();
            txtFullname.Text = dr["FullName"].ToString();
            if (dr["Gender"].Equals(0))
            {
                rdSex.SelectedValue = "0";
            }
            if (dr["Gender"].Equals(1))
                rdSex.SelectedIndex = 1;
            if (dr["Gender"].Equals(null))
                rdSex.SelectedValue="-1";
            txtPhone.Text = dr["Mobile"].ToString();
            txtAddress.Text = dr["Address"].ToString();
            txtIntroduce.Text = dr["About"].ToString();
            LoadDataToDropdownDate();
            LoadDataToDropdownMonth();
            LoadDataToDropdownYear();
            string birthday = dr["NgaySinh1"].ToString();
            if (String.IsNullOrEmpty(birthday) != true)
            {
                ddlDate.SelectedValue = birthday.Substring(0, 2);
                ddlMonth.SelectedValue = birthday.Substring(3, 2);
                ddlYear.SelectedValue = birthday.Substring(6, 4);
            }
          
            txtPhone.Text = dr["Mobile"].ToString();
            txtAddress.Text = dr["Address"].ToString();
            txtIntroduce.Text = dr["About"].ToString();


        }
        

    }
    protected void LoadDataToDropdownMonth()
    { 
        ListItem item;
        for (int i = 1; i <= 12; i++)
        {
            if(i<10)
            item = new ListItem(Convert.ToString("Tháng " + i), "0"+Convert.ToString(i));
            else
                item = new ListItem(Convert.ToString("Tháng " + i),Convert.ToString(i));
            ddlMonth.Items.Add(item);
        }
    }
    protected void LoadDataToDropdownDate()
    {
        ListItem item;
        for (int i = 1; i <= 31; i++)
        {
            if (i < 10)
                item = new ListItem("0" + Convert.ToString(i), Convert.ToString(i));
            else
            item = new ListItem(Convert.ToString(i), Convert.ToString(i));
            ddlDate.Items.Add(item);
        }
    }
    protected void LoadDataToDropdownYear()
    {
        ListItem item;
        for (int i = DateTime.Now.Year - 100; i <= DateTime.Now.Year; i++)
        {
            item = new ListItem(Convert.ToString(i), Convert.ToString(i));
            ddlYear.Items.Add(item);
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //try
        //{
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdateUsers";
            Dal.ParameterCollection = new string[8] { "@TenHienThi", "@HoTen", "@DienThoai", "@DiaChi", "@NgaySinh", "@GioiTinh", "@TuGioiThieu", "@UserEmail" };
            Dal.ValueCollection = new string[8] { txtDisplayName.Text, txtFullname.Text,txtPhone.Text,txtAddress.Text,( ddlMonth.SelectedValue +"/" +ddlDate.SelectedValue + "/" + ddlYear.SelectedValue), rdSex.SelectedValue,txtIntroduce.Text,Session["UserEmail"].ToString() };
            Dal.ExecuteNonQuery();
            ReloadData();
            lblInfo.Text = "Cập nhật thành công";
        //}
        //catch
        //{
            //lblInfo.Text = "Lỗi hệ thống, vui lòng cập nhật lại sau";
        //}
    }
    protected void ReloadData()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetUserInForByUserEmail";
        Dal.ParameterCollection = new string[1] { "@UserEmail" };
        Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
        DataRow dr = Dal.GetDataRow();
        avatar.ImageUrl = "~/Images/UserAvatar/" + dr["Image"].ToString();
        txtDisplayName.Text = dr["ShowName"].ToString();
        txtFullname.Text = dr["FullName"].ToString();
        if (dr["Gender"].Equals(false))
        {
            rdSex.SelectedValue = "0";
        }
        if (dr["Gender"].Equals(true))
            rdSex.SelectedIndex = 1;
        if (dr["Gender"].Equals(null))
            rdSex.SelectedValue = "-1";
        txtPhone.Text = dr["Mobile"].ToString();
        txtAddress.Text = dr["Address"].ToString();
        txtIntroduce.Text = dr["About"].ToString();
        LoadDataToDropdownDate();
        LoadDataToDropdownMonth();
        LoadDataToDropdownYear();
        txtPhone.Text = dr["Mobile"].ToString();
        txtAddress.Text = dr["Address"].ToString();
        txtIntroduce.Text = dr["About"].ToString();

    }
}
